package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/yyangl/yoyo/application"
	"github.com/yyangl/yoyo/middlwares"
	"github.com/yyangl/yoyo/module/admin/v1/controller"
)

type Admin struct {
	app     *application.Application
	Version string
}

var (
	apiMiddleware = []gin.HandlerFunc{
		//middlwares.PrometheusMiddleware(),
		middlwares.ServiceMiddleWare(),
		middlwares.AuthMiddleware("admin"),
		//middlwares.LbMiddleware(),
	}
)

func (m *Admin) SetApplication(app *application.Application) {
	m.app = app
}

/**

 */
func (m *Admin) BootStrap() {
	m.RegController(&controller.Login{}, middlwares.ServiceMiddleWare())
	m.RegController(&controller.Menu{}, apiMiddleware...)
	m.RegController(&controller.Promises{}, apiMiddleware...)
}

func (m *Admin) Name() string {
	return "admin"
}

func (m *Admin) GetVersion() string {
	if m.Version == "" {
		return application.DefaultVersion
	}
	return m.Version
}

/**
注册controller
*/
func (m *Admin) RegController(controller application.Controller, middlewares ...gin.HandlerFunc) {
	controller.SetApplication(m.app)
	router := m.app.GetRouterGroup("/" + m.Name() + "/" + m.GetVersion())
	router.Use(middlewares...)
	controller.SetRouter(router)
	controller.RegRouter()
}
